package com.im323.ip.impl;

import com.im323.common.util.HttpClientUtils;
import com.im323.common.util.IpModel;
import com.im323.common.util.IpUtil;
import com.im323.ip.IpConverter;

/**
 * 纯真IP
 * 
 * @author yejian
 *
 */
public class IpCZConvert extends IpConverter {

	@Override
	public IpModel parse() {
		long sta = System.currentTimeMillis();
		String doGet = HttpClientUtils.doGet(IpUtil.URL_CZ);
		IpModel ipModel = null;
		if (doGet != null) {
			int startIndex = doGet.indexOf("<span id=\"cz_ip\">") + 17;
			int endIndex = doGet.indexOf("<", startIndex);

			String ip = doGet.substring(startIndex, endIndex);

			startIndex = doGet.indexOf("<span id=\"cz_addr\">", endIndex) + 19;
			endIndex = doGet.indexOf("<", startIndex);

			String address = doGet.substring(startIndex, endIndex);
			String location = address.substring(0, address.indexOf(" "));
			String isp = address.replace(location, "").trim();

			ipModel = new IpModel();
			ipModel.setIpAddress(ip);
			ipModel.setLocation(location);
			ipModel.setIsp(isp);
			ipModel.setDelay(System.currentTimeMillis() - sta);
			ipModel.setUrl(IpUtil.URL_CZ);
		}
		return ipModel;
	}

}
